<div class="tableinfo">
    <div class="tableHeader">
        <p>Hộp thư</p>
    </div>
    
    
    <table border ="0" cellspacing ="0" cellpadding ="0">
        <tr style="background: silver; font-weight: bold;">
        <?php
        $user = $_SESSION['userlogin'];
        if($_SESSION['vaitro']=="Quản Trị"){
            $linkshowtn="../admin/hienthi_tinnhan.php?id=";
            $linkxoatn="<td align='center'><a href='../controller/xuly_xoa_tinnhan.php?id=$idtinnhan'title='Xóa tin nhắn'><img src='../images/delete_icon.png' alt='Xóa thông báo' /></a></td>";
            $td="<td>Xoá</td>";
        }else{
            $linkshowtn="../view/hienthi_tinnhan.php?id="; 
            $linkxoatn="";
            $td="";
        }?>
         
            <td>Tiêu đề </td>
            <td>ID </td>
            <td >Người gởi</td>
            <td >ngày</td>
            <td>Tình trạng</td>
            <?php echo $td; ?>
               
        </tr>
        
        <?php

        //so dong hien thi tren mot trang
        $hienthi = 10;
        //tinh tong so trang
        if(isset($_GET['page']) && (int)$_GET['page']){
            $page = $_GET['page'];
        }else{
            $sodong = "select NhanVienNhan from tinnhangui where NhanVienNhan = '$user'";
            $kq=executeQuery($sodong);
            $kq_sodong =mysql_num_rows($kq);
            $so = $kq_sodong;
            if($so>$hienthi){
                $page = ceil($so/$hienthi);
            }else{
                $page = 1;
            }
        }
        //vitri bat dau hien thi
        $vitri = (isset($_GET['vitri'])&& (int)$_GET['vitri']) ? $_GET['vitri']:0;            
            
             
            $sql = "SELECT a.id,a.TieuDe,a.id_NhanVien,c.HoTen,date_format(a.ngaygoi,'%d-%m-%Y') as ngaygoi,b.TinhTrang FROM  tinnhangui a inner join tinhtrangtinnhan b on a.MaTTTN_nguoinhan = b.id  inner join chitietnhanvien c on a.id_NhanVien = c.id_NhanVien where a.NhanVienNhan='$user'  ORDER BY  a.ngaygoi DESC limit $vitri,$hienthi";
            $dstinnhan = executeQuery($sql);
            
            while ($row = mysql_fetch_assoc($dstinnhan)){
               
            $idtinnhan = $row['id'];
            echo " <tr>
         
            <td><a href='$linkshowtn $idtinnhan' style='font-size: medium;'>".$row['TieuDe']."</a></td>
            <td>".$row['id_NhanVien']."</td>
            <td>".$row['HoTen']."</td>
            <td>".$row['ngaygoi']."</td>
            <td>".$row['TinhTrang']."</td>
            $linkxoatn

            </tr>" ;
            

            }
            $_SESSION['tinnhan']="tinnhan";
            echo "<tr><td colspan=6>Số tin nhắn chưa đọc: ".$sotn."</td></td>";
        ?>

    </table>

        <ul class="phantrang">
        
            <?php
    if($_SESSION['vaitro']=="Quản Trị"){
        //dành cho quản trị
              if($page >1){
                $sau = $vitri + $hienthi;
                $truoc = $vitri - $hienthi;
                $tranght= ($vitri/$hienthi)+1;

                //nut pre                
                if($tranght !=1){
                    echo "<li><a href='../admin/tinnhan.php?vitri=".$truoc."'>Trước</a></li>";
                }
                
                //so trang
                for($i=1; $i<=$page; $i++ ){
                    if($tranght != $i){
                        echo "<li><a href='../admin/tinnhan.php?vitri=".$hienthi*($i-1)."'>$i</a></li>"; 
                    }else{
                        echo "<li class='hientai'><a href=''>$i</a></li>";
                    } 
                }
                
                
                //nut next
                if($tranght != $page){
                    echo "<li><a href='../admin/tinnhan.php?vitri=".$sau."'>Sau</a></li>";
                }
              }        
    }else{
        //dành cho user
              if($page >1){
                $sau = $vitri + $hienthi;
                $truoc = $vitri - $hienthi;
                $tranght= ($vitri/$hienthi)+1;

                //nut pre                
                if($tranght !=1){
                    echo "<li><a href='../view/tinnhan.php?vitri=".$truoc."'>Trước</a></li>";
                }
                
                //so trang
                for($i=1; $i<=$page; $i++ ){
                    if($tranght != $i){
                        echo "<li><a href='../view/tinnhan.php?vitri=".$hienthi*($i-1)."'>$i</a></li>"; 
                    }else{
                        echo "<li class='hientai'><a href=''>$i</a></li>";
                    } 
                }
                
                
                //nut next
                if($tranght != $page){
                    echo "<li><a href='../view/tinnhan.php?vitri=".$sau."'>Sau</a></li>";
                }
              }        
    }            
               //echo $so; 
  
            ?>
             
        </ul>     
</div>
